//
//  PayMoneyDetailViewController.m
//  PropertyManager
//
//  Created by TNG on 2018/7/12.
//  Copyright © 2018年 Tng. All rights reserved.
//

#import "PayMoneyDetailViewController.h"

@interface PayMoneyDetailViewController () <UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) UITableView * tableview;
@property (nonatomic,assign) BOOL isHeadFresh;
@property (nonatomic,assign) int page;
@property (strong, nonatomic) NSMutableArray * dataArr;
@property (strong, nonatomic) NSMutableArray * overDueDataArr;

@end

@implementation PayMoneyDetailViewController

static NSString * cellID = @"cell";
static NSString * PayMoneyDetailTableViewCellIID = @"PayMoneyDetailTableViewCell";
static NSString * PayMoneyTableViewCellID = @"PayMoneyTableViewCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    self.page = 1;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"历史账单" style:0 target:self action:@selector(rightClick)];
    [self creatTableview];
    [self registCell];
    [self RequestData];
//    [self RequestNoPayData];
}

-(NSMutableArray *)dataArr
{
    if (_dataArr == nil) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

-(NSMutableArray *)overDueDataArr
{
    if (_overDueDataArr == nil) {
        _overDueDataArr = [NSMutableArray array];
    }
    return _overDueDataArr;
}

#pragma mark - Data
-(void)RequestData
{
    WeakSelf;
//    NSString * urlStr = [NSString stringWithFormat:@"rest/gbBqsftzdController/getList?pageno=1&pagesize=10&pkCustomerid=%@",SFMgbuserid];
    NSString * urlStr = [NSString stringWithFormat:@"/rest/gbBqsftzdController/getPay?pkCustomerid=%@&busreceivemonth=%@",SFMgbuserid,@"11"];
    [[HTTPClient shareClient] requestWithPath:urlStr AndReturnBlock:^(NSDictionary *dic) {
        if (weakSelf.isHeadFresh) {
            [weakSelf.dataArr removeAllObjects];
        }
        NSArray * dataArr = dic[@"data"][@"list"];
        for (NSDictionary * dics in dataArr) {
            PayMoneyModel * model = [PayMoneyModel modelWithDic:dics];
            [weakSelf.dataArr addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf.tableview reloadData];
        });
    } AndFailMessage:^(NSString *message, NSString *errorCode) {
        [SVProgressHUD ShowErrorMessage:message];
    }];
}

#pragma mark - Data
-(void)RequestNoPayData
{
    WeakSelf;
    //rest/gbBqsftzdController/getPay?pkCustomerid=&busreceivemonth=

//    NSString * urlStr = [NSString stringWithFormat:@"/rest/gbYqwjqdController/getList?pageno=%d&pagesize=10&pkCustomerid=%@",self.page,SFMgbuserid];
    NSString * urlStr = [NSString stringWithFormat:@"/rest/gbBqsftzdController/getPay?pkCustomerid=%@&busreceivemonth=%@",SFMgbuserid,@"11"];
    
    [[HTTPClient shareClient] requestWithPath:urlStr AndReturnBlock:^(NSDictionary *dic) {
        if (weakSelf.isHeadFresh) {
            [weakSelf.dataArr removeAllObjects];
        }
        NSDictionary * dics = dic[@"data"];
        NSArray * dataArr = dics[@"list"];
        for (NSDictionary * dics in dataArr) {
            overDueNoPayModel * model = [overDueNoPayModel modelWithDic:dics];
            [weakSelf.overDueDataArr addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf.tableview reloadData];
        });
    } AndFailMessage:^(NSString *message, NSString *errorCode) {
        [SVProgressHUD ShowErrorMessage:message];
    }];
}

-(void)rightClick
{
    PayMoneyRecodeViewController * ctr = [[PayMoneyRecodeViewController alloc] init];
    [self.navigationController pushViewController:ctr animated:YES];
}

-(void)registCell
{
    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:cellID];
    [self.tableview registerClass:[PayMoneyDetailTableViewCell class] forCellReuseIdentifier:PayMoneyDetailTableViewCellIID];
    [self.tableview registerClass:[PayMoneyTableViewCell class] forCellReuseIdentifier:PayMoneyTableViewCellID];
}

-(void)creatTableview{
    if (self.tableview == nil) {
        UITableView * tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, kStatusAndNavBarH, WIDTH, HEIGHT - kStatusAndNavBarH - kHomeLineH) style:UITableViewStyleGrouped];
        tableview.delegate = self;
        tableview.dataSource = self;
        tableview.tableFooterView = [UIView new];
        if (@available(iOS 11.0, *)) {
            tableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
        }
        self.tableview = tableview;
        [self.view addSubview:self.tableview];
    }
    
    // 下拉刷新
    WeakSelf;
    self.tableview.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.isHeadFresh = YES;
        weakSelf.page = 1;
        [weakSelf RequestData];
        [weakSelf.tableview.mj_header endRefreshing];
    }];
    // 设置自动切换透明度(在导航栏下面自动隐藏)
    self.tableview.mj_header.automaticallyChangeAlpha = YES;
    
    self.tableview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        // 结束刷新
        weakSelf.isHeadFresh = NO;
        weakSelf.page += 1;
        [weakSelf RequestData];
        [weakSelf.tableview.mj_footer endRefreshing];
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 118;
    }
    return 70;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return self.dataArr.count;
    }
    return self.overDueDataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        PayMoneyDetailTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:PayMoneyDetailTableViewCellIID];
        if (self.dataArr.count) {
            PayMoneyModel * model = [self.dataArr objectAtIndex:indexPath.row];
            cell.moneyLabel.text = [NSString stringWithFormat:@"¥%@",model.nyreceivable];
        }
        return cell;
    }
    PayMoneyTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:PayMoneyTableViewCellID];
    if (self.overDueDataArr.count) {
        overDueNoPayModel * model = [self.overDueDataArr objectAtIndex:indexPath.row];
        [cell sendoverDueNoPayModel:model];
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    PayMoneyLatestDetailViewController * ctr = [[PayMoneyLatestDetailViewController alloc] init];
//    PayMoneyDetailZuiXinViewController * ctr = [[PayMoneyDetailZuiXinViewController alloc] init];

    ctr.isPay = YES;
    if (indexPath.section == 0) {
        PayMoneyModel * model = [self.dataArr objectAtIndex:indexPath.row];
        ctr.model = model;
    }else
    {
        overDueNoPayModel * model = [self.overDueDataArr objectAtIndex:indexPath.row];
        ctr.overDueNoPayModel = model;
    }
    [self.navigationController pushViewController:ctr animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        if (self.overDueDataArr.count) {
            return 40;
        }
    }
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        if (self.overDueDataArr.count) {
            UIView * bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 40)];
            bgView.backgroundColor = SFM242;
            
            UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, WIDTH - 30, 20)];
            titleLabel.font = font(14);
            titleLabel.textColor = SFM51;
            titleLabel.textAlignment = NSTextAlignmentLeft;
            titleLabel.text = [NSString stringWithFormat:@"未缴费账单共%d笔",(int)self.overDueDataArr.count];
            [bgView addSubview:titleLabel];
            return bgView;
        }
    }
    return [UIView new];
}

-(BOOL)hidesBottomBarWhenPushed
{
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
